//
//  UIView+AutoLayout.m
//  PAEBank
//
//  Created by Peng on 2018/3/23.
//  Copyright © 2018年 PingAn. All rights reserved.
//

#import "UIView+AutoLayout.h"

@implementation UIView (AutoLayout)

- (void)addSubview:(UIView *)view toViewController:(UIViewController *)vc{
    [self addSubview:view];
    view.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray *consHArr =  [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"view": view}];
    [self addConstraints:consHArr];
    NSArray *consVArr =  [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(a)-[view]-(b)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:@{@"a": vc.topLayoutGuide.bottomAnchor,@"b": vc.bottomLayoutGuide.topAnchor} views:@{@"view": view}];
    [self addConstraints:consVArr];
}


- (void)addWidth:(CGFloat)width{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width];
    [self addConstraint:cons];
}


- (void)addHeight:(CGFloat)height{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height];
    [self addConstraint:cons];

}


- (void)equalWidthTo:(UIView *)view{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
    [self sy_addConstrain:cons toView:view];
}


- (void)equalHeightTo:(UIView *)view{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    [self sy_addConstrain:cons toView:view];
}


- (void)addSubview:(UIView *)view withInsets:(UIEdgeInsets)insets{
    [self addSubview:view];
    view.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray *consHArr =  [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(a)-[view]-(b)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:@{@"a": @(insets.left),@"b": @(insets.right)} views:@{@"view": view}];
    [self addConstraints:consHArr];
    NSArray *consVArr =  [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(a)-[view]-(b)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:@{@"a": @(insets.top),@"b": @(insets.bottom)} views:@{@"view": view}];
    [self addConstraints:consVArr];
}


- (void)topToTopWithView:(UIView *)view andInterval:(CGFloat) interval{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1.0 constant:interval];
    [self sy_addConstrain:cons toView:view];
}


- (void)topToBottomWithView:(UIView *)view andInterval:(CGFloat) interval{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:interval];
    [self sy_addConstrain:cons toView:view];
}


- (void)bottomToBottomWithView:(UIView *)view andInterval:(CGFloat)interval{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:interval];
    [self sy_addConstrain:cons toView:view];

}


- (void)leftToLeftWithView:(UIView *)view andInterval:(CGFloat)interval{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:interval];
    [self sy_addConstrain:cons toView:view];
}


- (void)leftToRightWithView:(UIView *)view andInterval:(CGFloat)interval{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeRight multiplier:1.0 constant:interval];
    [self sy_addConstrain:cons toView:view];
}


- (void)centerXToCenterXWithView:(UIView *)view andInterval:(CGFloat)interval{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:interval];
    [self sy_addConstrain:cons toView:view];
}


- (void)centerYToCenterYWithView:(UIView *)view andInterval:(CGFloat)interval{
    NSLayoutConstraint *cons = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:interval];
    [self sy_addConstrain:cons toView:view];
}

#pragma mark - Private Func


/**
 确保相互作用关系的约束添加在父视图上

 @param cons 约束条件
 @param view 传入的视图
 */
- (void)sy_addConstrain:(NSLayoutConstraint *)cons toView:(UIView *)view{
    if (self.superview == view) {
        [view addConstraint:cons];
    }else if (self.superview == self){
        [self addConstraint:cons];
    }else{
        [self.superview addConstraint:cons];
    }
}


@end
